home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / shells / sh03src.zoo / sh-pl03 / sh / var.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-18  |  12.9 KB  |  653 lines

  1. /*-
  2.  * Copyright (c) 1991 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Kenneth Almquist.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #ifndef lint
  38. static char sccsid[] = "@(#)var.c    5.3 (Berkeley) 4/12/91";
  39. #endif /* not lint */
  40.  
  41. /*
  42.  * Shell variables.
  43.  */
  44.  
  45. #include "shell.h"
  46. #include "output.h"
  47. #include "expand.h"
  48. #include "nodes.h"    /* for other headers */
  49. #include "eval.h"    /* defines cmdenviron */
  50. #include "exec.h"
  51. #include "syntax.h"
  52. #include "options.h"
  53. #include "mail.h"
  54. #include "var.h"
  55. #include "memalloc.h"
  56. #include "error.h"
  57. #include "mystring.h"
  58.  
  59.  
  60. #define VTABSIZE 39
  61.  
  62.  
  63. struct varinit {
  64.     struct var *var;
  65.     int flags;
  66.     char *text;
  67. };
  68.  
  69.  
  70. #if ATTY
  71. struct var vatty;
  72. #endif
  73. struct var vifs;
  74. struct var vmail;
  75. struct var vmpath;
  76. struct var vpath;
  77. struct var vps1;
  78. struct var vps2;
  79. struct var vvers;
  80. #if ATTY
  81. struct var vterm;
  82. #endif
  83.  
  84. #ifndef __MBASE__
  85. const 
  86. #endif
  87. struct varinit varinit[] = {
  88. #if ATTY
  89.     {&vatty,    VSTRFIXED|VTEXTFIXED|VUNSET,    "ATTY="},
  90. #endif
  91.     {&vifs,    VSTRFIXED|VTEXTFIXED,        "IFS= \t\n"},
  92.     {&vmail,    VSTRFIXED|VTEXTFIXED|VUNSET,    "MAIL="},
  93.     {&vmpath,    VSTRFIXED|VTEXTFIXED|VUNSET,    "MAILPATH="},
  94.     {&vpath,    VSTRFIXED|VTEXTFIXED,        "PATH=:/bin:/usr/bin"},
  95.     /* 
  96.      * vps1 depends on uid
  97.      */
  98.     {&vps2,    VSTRFIXED|VTEXTFIXED,        "PS2=> "},
  99. #if ATTY
  100.     {&vterm,    VSTRFIXED|VTEXTFIXED|VUNSET,    "TERM="},
  101. #endif
  102.     {NULL,    0,                NULL}
  103. };
  104.  
  105. struct var *vartab[VTABSIZE];
  106.  
  107. STATIC void unsetvar __P((char *));
  108. STATIC struct var **hashvar __P((char *));
  109. STATIC int varequal __P((char *, char *));
  110.  
  111. /*
  112.  * Initialize the varable symbol tables and import the environment
  113.  */
  114.  
  115. #ifdef mkinit
  116. INCLUDE "var.h"
  117. INIT {
  118.     char **envp;
  119.     extern char **environ;
  120.  
  121.     initvar();
  122.     for (envp = environ ; *envp ; envp++) {
  123.         if (strchr(*envp, '=')) {
  124.             setvareq(*envp, VEXPORT|VTEXTFIXED);
  125.         }
  126.     }
  127. }
  128. #endif
  129.  
  130.  
  131. /*
  132.  * This routine initializes the builtin variables.  It is called when the
  133.  * shell is initialized and again when a shell procedure is spawned.
  134.  */
  135.  
  136. void
  137. initvar() {
  138.     const struct varinit *ip;
  139.     struct var *vp;
  140.     struct var **vpp;
  141.  
  142.     for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
  143.         if ((vp->flags & VEXPORT) == 0) {
  144.             vpp = hashvar(ip->text);
  145.             vp->next = *vpp;
  146.             *vpp = vp;
  147.             vp->text = ip->text;
  148.             vp->flags = ip->flags;
  149.         }
  150.     }
  151.     /*
  152.      * PS1 depends on uid
  153.      */
  154.     if ((vps1.flags & VEXPORT) == 0) {
  155.         vpp = hashvar("PS1=");
  156.         vps1.next = *vpp;
  157.         *vpp = &vps1;
  158.         vps1.text = getuid() ? "PS1=$ " : "PS1=# ";
  159.         vps1.flags = VSTRFIXED|VTEXTFIXED;
  160.     }
  161. }
  162.  
  163. /*
  164.  * Set the value of a variable.  The flags argument is ored with the
  165.  * flags of the variable.  If val is NULL, the variable is unset.
  166.  */
  167.  
  168. void
  169. setvar(name, val, flags)
  170.     char *name, *val;
  171.     {
  172.     char *p, *q;
  173.     int len;
  174.     int namelen;
  175.     char *nameeq;
  176.     int isbad;
  177.  
  178.     isbad = 0;
  179.     p = name;
  180.     if (! is_name(*p++))
  181.         isbad = 1;
  182.     for (;;) {
  183.         if (! is_in_name(*p)) {
  184.             if (*p == '\0' || *p == '=')
  185.                 break;
  186.             isbad = 1;
  187.         }
  188.         p++;
  189.     }
  190.     namelen = p - name;
  191.     if (isbad)
  192.         error("%.*s: is read only", namelen, name);
  193.     len = namelen + 2;        /* 2 is space for '=' and '\0' */
  194.     if (val == NULL) {
  195.         flags |= VUNSET;
  196.     } else {
  197.         len += strlen(val);
  198.     }
  199.     p = nameeq = ckmalloc(len);
  200.     q = name;
  201.     while (--namelen >= 0)
  202.         *p++ = *q++;
  203.     *p++ = '=';
  204.     *p = '\0';
  205.     if (val)
  206.         scopy(val, p);
  207.     setvareq(nameeq, flags);
  208. }
  209.  
  210.  
  211.  
  212. /*
  213.  * Same as setvar except that the variable and value are passed in
  214.  * the first argument as name=value.  Since the first argument will
  215.  * be actually stored in the table, it should not be a string that
  216.  * will go away.
  217.  */
  218.  
  219. void
  220. setvareq(s, flags)
  221.     char *s;
  222.     {
  223.     struct var *vp, **vpp;
  224.  
  225.     vpp = hashvar(s);
  226.     for (vp = *vpp ; vp ; vp = vp->next) {
  227.         if (varequal(s, vp->text)) {
  228.             if (vp->flags & VREADONLY) {
  229.                 int len = strchr(s, '=') - s;
  230.                 error("%.*s: is read only", len, s);
  231.             }
  232.             INTOFF;
  233.             if (vp == &vpath)
  234.                 changepath(s + 5);    /* 5 = strlen("PATH=") */
  235.             if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
  236.                 ckfree(vp->text);
  237.             vp->flags &=~ (VTEXTFIXED|VSTACK|VUNSET);
  238.             vp->flags |= flags;
  239.             vp->text = s;
  240.             if (vp == &vmpath || (vp == &vmail && ! mpathset()))
  241.                 chkmail(1);
  242.             INTON;
  243.             return;
  244.         }
  245.     }
  246.     /* not found */
  247.     vp = ckmalloc(sizeof (*vp));
  248.     vp->flags = flags;
  249.     vp->text = s;
  250.     vp->next = *vpp;
  251.     *vpp = vp;
  252. }
  253.  
  254.  
  255.  
  256. /*
  257.  * Process a linked list of variable assignments.
  258.  */
  259.  
  260. void
  261. listsetvar(list)
  262.     struct strlist *list;
  263.     {
  264.     struct strlist *lp;
  265.  
  266.     INTOFF;
  267.     for (lp = list ; lp ; lp = lp->next) {
  268.         setvareq(savestr(lp->text), 0);
  269.     }
  270.     INTON;
  271. }
  272.  
  273.  
  274.  
  275. /*
  276.  * Find the value of a variable.  Returns NULL if not set.
  277.  */
  278.  
  279. char *
  280. lookupvar(name)
  281.     char *name;
  282.     {
  283.     struct var *v;
  284.  
  285.     for (v = *hashvar(name) ; v ; v = v->next) {
  286.         if (varequal(v->text, name)) {
  287.             if (v->flags & VUNSET)
  288.                 return NULL;
  289.             return strchr(v->text, '=') + 1;
  290.         }
  291.     }
  292.     return NULL;
  293. }
  294.  
  295.  
  296.  
  297. /*
  298.  * Search the environment of a builtin command.  If the second argument
  299.  * is nonzero, return the value of a variable even if it hasn't been
  300.  * exported.
  301.  */
  302.  
  303. char *
  304. bltinlookup(name, doall)
  305.     char *name;
  306.     {
  307.     struct strlist *sp;
  308.     struct var *v;
  309.  
  310.     for (sp = cmdenviron ; sp ; sp = sp->next) {
  311.         if (varequal(sp->text, name))
  312.             return strchr(sp->text, '=') + 1;
  313.     }
  314.     for (v = *hashvar(name) ; v ; v = v->next) {
  315.         if (varequal(v->text, name)) {
  316.             if (v->flags & VUNSET
  317.              || ! doall && (v->flags & VEXPORT) == 0)
  318.                 return NULL;
  319.             return strchr(v->text, '=') + 1;
  320.         }
  321.     }
  322.     return NULL;
  323. }
  324.  
  325.  
  326.  
  327. /*
  328.  * Generate a list of exported variables.  This routine is used to construct
  329.  * the third argument to execve when executing a program.
  330.  */
  331.  
  332. char **
  333. environment() {
  334.     int nenv;
  335.     struct var **vpp;
  336.     struct var *vp;
  337.     char **env, **ep;
  338.  
  339.     nenv = 0;
  340.     for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
  341.         for (vp = *vpp ; vp ; vp = vp->next)
  342.             if (vp->flags & VEXPORT)
  343.                 nenv++;
  344.     }
  345.     ep = env = stalloc((nenv + 1) * sizeof *env);
  346.     for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
  347.         for (vp = *vpp ; vp ; vp = vp->next)
  348.             if (vp->flags & VEXPORT)
  349.                 *ep++ = vp->text;
  350.     }
  351.     *ep = NULL;
  352.     return env;
  353. }
  354.  
  355.  
  356. /*
  357.  * Called when a shell procedure is invoked to clear out nonexported
  358.  * variables.  It is also necessary to reallocate variables of with
  359.  * VSTACK set since these are currently allocated on the stack.
  360.  */
  361.  
  362. #ifdef mkinit
  363. MKINIT void shprocvar();
  364.  
  365. SHELLPROC {
  366.     shprocvar();
  367. }
  368. #endif
  369.  
  370. void
  371. shprocvar() {
  372.     struct var **vpp;
  373.     struct var *vp, **prev;
  374.  
  375.     for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
  376.         for (prev = vpp ; (vp = *prev) != NULL ; ) {
  377.             if ((vp->flags & VEXPORT) == 0) {
  378.                 *prev = vp->next;
  379.                 if ((vp->flags & VTEXTFIXED) == 0)
  380.                     ckfree(vp->text);
  381.                 if ((vp->flags & VSTRFIXED) == 0)
  382.                     ckfree(vp);
  383.             } else {
  384.                 if (vp->flags & VSTACK) {
  385.                     vp->text = savestr(vp->text);
  386.                     vp->flags &=~ VSTACK;
  387.                 }
  388.                 prev = &vp->next;
  389.             }
  390.         }
  391.     }
  392.     initvar();
  393. }
  394.  
  395.  
  396.  
  397. /*
  398.  * Command to list all variables which are set.  Currently this command
  399.  * is invoked from the set command when the set command is called without
  400.  * any variables.
  401.  */
  402.  
  403. int
  404. showvarscmd(argc, argv)  char **argv; {
  405.     struct var **vpp;
  406.     struct var *vp;
  407.  
  408.     for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
  409.         for (vp = *vpp ; vp ; vp = vp->next) {
  410.             if ((vp->flags & VUNSET) == 0)
  411.                 out1fmt("%s\n", vp->text);
  412.         }
  413.     }
  414.     return 0;
  415. }
  416.  
  417.  
  418.  
  419. /*
  420.  * The export and readonly commands.
  421.  */
  422.  
  423. int
  424. exportcmd(argc, argv)  char **argv; {
  425.     struct var **vpp;
  426.     struct var *vp;
  427.     char *name;
  428.     char *p;
  429.     int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
  430.  
  431.     listsetvar(cmdenviron);
  432.     if (argc > 1) {
  433.         while ((name = *argptr++) != NULL) {
  434.             if ((p = strchr(name, '=')) != NULL) {
  435.                 p++;
  436.             } else {
  437.                 vpp = hashvar(name);
  438.                 for (vp = *vpp ; vp ; vp = vp->next) {
  439.                     if (varequal(vp->text, name)) {
  440.                         vp->flags |= flag;
  441.                         goto found;
  442.                     }
  443.                 }
  444.             }
  445.             setvar(name, p, flag);
  446. found:;
  447.         }
  448.     } else {
  449.         for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
  450.             for (vp = *vpp ; vp ; vp = vp->next) {
  451.                 if (vp->flags & flag) {
  452.                     for (p = vp->text ; *p != '=' ; p++)
  453.                         out1c(*p);
  454.                     out1c('\n');
  455.                 }
  456.             }
  457.         }
  458.     }
  459.     return 0;
  460. }
  461.  
  462.  
  463. /*
  464.  * The "local" command.
  465.  */
  466.  
  467. localcmd(argc, argv)  char **argv; {
  468.     char *name;
  469.  
  470.     if (! in_function())
  471.         error("Not in a function");
  472.     while ((name = *argptr++) != NULL) {
  473.         mklocal(name);
  474.     }
  475.     return 0;
  476. }
  477.  
  478.  
  479. /*
  480.  * Make a variable a local variable.  When a variable is made local, it's
  481.  * value and flags are saved in a localvar structure.  The saved values
  482.  * will be restored when the shell function returns.  We handle the name
  483.  * "-" as a special case.
  484.  */
  485.  
  486. void
  487. mklocal(name)
  488.     char *name;
  489.     {
  490.     struct localvar *lvp;
  491.     struct var **vpp;
  492.     struct var *vp;
  493.  
  494.     INTOFF;
  495.     lvp = ckmalloc(sizeof (struct localvar));
  496.     if (name[0] == '-' && name[1] == '\0') {
  497.         lvp->text = ckmalloc(sizeof optval);
  498.         bcopy(optval, lvp->text, sizeof optval);
  499.         vp = NULL;
  500.     } else {
  501.         vpp = hashvar(name);
  502.         for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
  503.         if (vp == NULL) {
  504.             if (strchr(name, '='))
  505.                 setvareq(savestr(name), VSTRFIXED);
  506.             else
  507.                 setvar(name, NULL, VSTRFIXED);
  508.             vp = *vpp;    /* the new variable */
  509.             lvp->text = NULL;
  510.             lvp->flags = VUNSET;
  511.         } else {
  512.             lvp->text = vp->text;
  513.             lvp->flags = vp->flags;
  514.             vp->flags |= VSTRFIXED|VTEXTFIXED;
  515.             if (strchr(name, '='))
  516.                 setvareq(savestr(name), 0);
  517.         }
  518.     }
  519.     lvp->vp = vp;
  520.     lvp->next = localvars;
  521.     localvars = lvp;
  522.     INTON;
  523. }
  524.  
  525.  
  526. /*
  527.  * Called after a function returns.
  528.  */
  529.  
  530. void
  531. poplocalvars() {
  532.     struct localvar *lvp;
  533.     struct var *vp;
  534.  
  535.     while ((lvp = localvars) != NULL) {
  536.         localvars = lvp->next;
  537.         vp = lvp->vp;
  538.         if (vp == NULL) {    /* $- saved */
  539.             bcopy(lvp->text, optval, sizeof optval);
  540.             ckfree(lvp->text);
  541.         } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
  542.             unsetvar(vp->text);
  543.         } else {
  544.             if ((vp->flags & VTEXTFIXED) == 0)
  545.                 ckfree(vp->text);
  546.             vp->flags = lvp->flags;
  547.             vp->text = lvp->text;
  548.         }
  549.         ckfree(lvp);
  550.     }
  551. }
  552.  
  553.  
  554. setvarcmd(argc, argv)  char **argv; {
  555.     if (argc <= 2)
  556.         return unsetcmd(argc, argv);
  557.     else if (argc == 3)
  558.         setvar(argv[1], argv[2], 0);
  559.     else
  560.         error("List assignment not implemented");
  561.     return 0;
  562. }
  563.  
  564.  
  565. /*
  566.  * The unset builtin command.  We unset the function before we unset the
  567.  * variable to allow a function to be unset when there is a readonly variable
  568.  * with the same name.
  569.  */
  570.  
  571. unsetcmd(argc, argv)  char **argv; {
  572.     char **ap;
  573.  
  574.     for (ap = argv + 1 ; *ap ; ap++) {
  575.         unsetfunc(*ap);
  576.         unsetvar(*ap);
  577.     }
  578.     return 0;
  579. }
  580.  
  581.  
  582. /*
  583.  * Unset the specified variable.
  584.  */
  585.  
  586. STATIC void
  587. unsetvar(s)
  588.     char *s;
  589.     {
  590.     struct var **vpp;
  591.     struct var *vp;
  592.  
  593.     vpp = hashvar(s);
  594.     for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
  595.         if (varequal(vp->text, s)) {
  596.             INTOFF;
  597.             if (*(strchr(vp->text, '=') + 1) != '\0'
  598.              || vp->flags & VREADONLY) {
  599.                 setvar(s, nullstr, 0);
  600.             }
  601.             vp->flags &=~ VEXPORT;
  602.             vp->flags |= VUNSET;
  603.             if ((vp->flags & VSTRFIXED) == 0) {
  604.                 if ((vp->flags & VTEXTFIXED) == 0)
  605.                     ckfree(vp->text);
  606.                 *vpp = vp->next;
  607.                 ckfree(vp);
  608.             }
  609.             INTON;
  610.             return;
  611.         }
  612.     }
  613. }
  614.  
  615.  
  616.  
  617. /*
  618.  * Find the appropriate entry in the hash table from the name.
  619.  */
  620.  
  621. STATIC struct var **
  622. hashvar(p)
  623.     register char *p;
  624.     {
  625.     unsigned int hashval;
  626.  
  627.     hashval = *p << 4;
  628.     while (*p && *p != '=')
  629.         hashval += *p++;
  630.     return &vartab[hashval % VTABSIZE];
  631. }
  632.  
  633.  
  634.  
  635. /*
  636.  * Returns true if the two strings specify the same varable.  The first
  637.  * variable name is terminated by '='; the second may be terminated by
  638.  * either '=' or '\0'.
  639.  */
  640.  
  641. STATIC int
  642. varequal(p, q)
  643.     register char *p, *q;
  644.     {
  645.     while (*p == *q++) {
  646.         if (*p++ == '=')
  647.             return 1;
  648.     }
  649.     if (*p == '=' && *(q - 1) == '\0')
  650.         return 1;
  651.     return 0;
  652. }
  653.